package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired
    private VodService vodService;


    /**
     * 将音频文件上传到云点播平台
     *
     * @param file
     * @return
     */
    @PostMapping("/trackInfo/uploadTrack")
    @Operation(summary = "将音频文件上传到云点播平台")
    public Result<Map<String, String>> uploadTrack(@RequestParam("file") MultipartFile file) {
        Map<String, String> map = vodService.uploadTrack(file);
        return Result.ok(map);
    }

    @GuiguLogin
    @PostMapping("/trackInfo/saveTrackInfo")
    @Operation(summary = "保存声音")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        trackInfoService.saveTrackInfo(trackInfoVo,userId);


        return Result.ok();
    }


    /**
     * 条件分页查询当前用户声音列表（包含声音统计信息）
     *
     * @param page           页码
     * @param limit          页大小
     * @param trackInfoQuery 查询条件
     * @return
     */
    @GuiguLogin
    @PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
    @Operation(summary = "条件分页查询当前用户声音列表（包含声音统计信息")
    public Result<Page<TrackListVo>> findUserTrackPage(
			@PathVariable int page,
			@PathVariable int limit,
			@RequestBody TrackInfoQuery trackInfoQuery) {


        Page<TrackListVo> page1 = new Page<>(page,limit);
		page1 = trackInfoService.findUserTrackPage(page1,trackInfoQuery);

		return Result.ok(page1);
    }


    /**
     * 查询声音信息,用于修改声音时回显数据
     * @param id
     * @return
     */
    @Operation(summary = "查询声音信息")
    @GuiGuCache(prefix = "album:trackInfo:")
    @GuiguLogin
    @GetMapping("/trackInfo/getTrackInfo/{id}")
    public Result<TrackInfo> getTrackInfo(@PathVariable(name = "id") Long id){

        Long userId = AuthContextHolder.getUserId();

        TrackInfo trackInfo = trackInfoService.getTrackInfo(id,userId);

        return Result.ok(trackInfo);
    }


    /**
     * 修改声音信息
     * @param id
     * @param trackInfo
     * @return
     */
    @GuiguLogin
    @PutMapping("/trackInfo/updateTrackInfo/{id}")
    @Operation(summary = "修改声音信息")
    public Result updateTrackInfo(@PathVariable(name = "id") Long id,
                                  @RequestBody TrackInfo trackInfo){

        Long userId = AuthContextHolder.getUserId();

        trackInfoService.updateTrackInfo(id,trackInfo,userId);

        return Result.ok();
    }


    /**
     * 删除声音信息
     * @param id
     * @return
     */
    @GuiguLogin
    @DeleteMapping("/api/album/trackInfo/removeTrackInfo/{id}")
    @Operation(summary = "删除声音信息")
    public Result removeTrackInfo(@PathVariable(name = "id") Long id){

        trackInfoService.removeTrackInfo(id);

        return Result.ok();
    }


    /**
     * 需求：用户未登录，可以给用户展示声音列表；用户已登录，可以给用户展示声音列表，并动态渲染付费标识
     * 分页查询专辑下声音列表（动态渲染付费标识）
     *
     * @param albumId
     * @param page
     * @param limit
     * @return
     */
    @GuiguLogin(required = false)
    @Operation(summary = "查询专辑声音分页列表")
    @GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(
            @PathVariable(value = "albumId") Long albumId,
            @PathVariable(value = "page") Integer page,
            @PathVariable(value = "limit") Integer limit
    ){

        //获取登录用户id
        Long userId = AuthContextHolder.getUserId();

        Page<AlbumTrackListVo> voPage = new Page<>(page, limit);

        voPage = trackInfoService.findAlbumTrackPage(albumId,voPage,userId);


        return Result.ok(voPage);
    }

    /**
     * 获取声音统计信息
     * @param trackId
     * @return
     */
    @Operation(summary = "获取声音统计信息")
    @GetMapping("/trackInfo/getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable(value = "trackId") Long trackId){

        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);

        return Result.ok(trackStatVo);
    }



    /**
     * 基于用户选择声音作为起始标准获取未购买声音数量，得到分集购买列表
     *
     * @param trackId
     * @return [{name:"本集",price:0.1,trackCount:1},{name:"后10集",price:1,trackCount:10},...,{name:"全集",price:4.3,trackCount:43},]
     */
    @GuiguLogin
    @Operation(summary = "基于用户选择声音作为起始标准获取未购买声音数量，得到分集购买列表")
    @GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
    public Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable Long trackId) {
        Long userId = AuthContextHolder.getUserId();
        List<Map<String, Object>> list = trackInfoService.findUserTrackPaidList(userId, trackId);
        return Result.ok(list);
    }


    /**
     * 基于选择购买声音ID作为起始获取指定数量未购买声音列表
     *
     * @param trackId
     * @param trackCount
     * @return
     */
    @GuiguLogin
    @Operation(summary = "基于选择购买声音ID作为起始获取指定数量未购买声音列表")
    @GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> findWaitBuyTrackList(@PathVariable Long trackId, @PathVariable Integer trackCount) {
        Long userId = AuthContextHolder.getUserId();
        List<TrackInfo> list = trackInfoService.findPaidTrackInfoList(userId, trackId, trackCount);
        return Result.ok(list);
    }
}

